﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using com.SFA.Server.Entity;
using Jumpton.Persistent;

public partial class masterdata_Customer_Product_Choose : PageBase
{
    WareHouseFromProductManager manager = new WareHouseFromProductManager();
    private const string REQUEST_HOUSE_ID = "id";
    private int _ID = 0;
    ObjectTable ot;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[REQUEST_HOUSE_ID]!=null)
        {
            int.TryParse(Request.QueryString[REQUEST_HOUSE_ID],out _ID);
        }
        if (!IsPostBack)
        {
            GridPager1.GridView = GridView1;
            GridPager1.CurrentPage = PageIndex;
            InitForm();
            ddlSelected.SelectedValue = "1";
            GetProductsBind();
            ddlSelected.SelectedValue = "0";
        }
    }

    private void InitForm()
    {
        GetManuType();
        GetCategoryType();
        GetBrandType();
        if (_ID > 0)
        {
            WareHouseManager manager = new WareHouseManager();
            WAREHOUSE ShipFrom = manager.Load(_ID);
            if (ShipFrom != null)
            {
                lblWarehousename.Text = ShipFrom.DESCRIPTION;
                hdfWareHouseCode.Value = ShipFrom.CODE;
            }
        }

    }

    #region 绑定品牌
    public void GetBrandType()
    {
        ddlBrand.DataTextField = "BrandName";
        ddlBrand.DataValueField = "BrandCode";
        ot = BrandManager.GetBrands(MyStateManager.DomainID);
        if (ot != null)
        {
            ot.BillDataTable.DefaultView.RowFilter = "ACTIVE=1";
            ddlBrand.DataSource = ot.BillDataTable.DefaultView;
            ddlBrand.DataBind();
        }
        ddlBrand.Items.Insert(0, new ListItem("--所有--", ""));
    }
    #endregion

    #region 绑定制造商
    public void GetManuType()
    {
        ddlManu.DataTextField = "ManuName";
        ddlManu.DataValueField = "ManuCode";
        ot = ManufacturersManager.GetManus(MyStateManager.DomainID);
        if (ot != null)
        {
            ot.BillDataTable.DefaultView.RowFilter = "ACTIVE=1";
            ddlManu.DataSource = ot.BillDataTable.DefaultView;
            ddlManu.DataBind();
        }
        ddlManu.Items.Insert(0, new ListItem("--所有--", ""));
    }
    #endregion

    #region 绑定品类
    public void GetCategoryType()
    {
        ddlCategroy.DataTextField = "CategoryName";
        ddlCategroy.DataValueField = "CategoryCode";
        ot = CategoryManager.GetCategories(MyStateManager.DomainID);
        if (ot != null)
        {
            ot.BillDataTable.DefaultView.RowFilter = "ACTIVE=1";
            ddlCategroy.DataSource = ot.BillDataTable.DefaultView;
            ddlCategroy.DataBind();
        }
        ddlCategroy.Items.Insert(0, new ListItem("--所有--", ""));
    }
    #endregion

    #region 制造商改变事件
    protected void ddlManu_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlBrand.DataTextField = "BrandName";
        ddlBrand.DataValueField = "BrandCode";
        DataSet ds = BrandManager.GetBrandsListDaynamic(ddlCategroy.SelectedValue,ddlManu.SelectedValue, MyStateManager.DomainID);
        ddlBrand.DataSource = ds;
        ddlBrand.DataBind();
        ddlBrand.Items.Insert(0, new ListItem("--所有--", ""));
    }
    #endregion

    #region 品类改变事件
    protected void ddlCategroy_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlBrand.DataTextField = "BrandName";
        ddlBrand.DataValueField = "BrandCode";
        DataSet ds = BrandManager.GetBrandsListDaynamic(ddlCategroy.SelectedValue,ddlManu.SelectedValue, MyStateManager.DomainID);
        ddlBrand.DataSource = ds;
        ddlBrand.DataBind();
        ddlBrand.Items.Insert(0, new ListItem("--所有--", ""));
    }
    #endregion

    public void GetProductsBind()
    {
        DataSet ds = ProductManager.GetWareHouseProducts(hdfWareHouseCode.Value,
                                                                    ddlCategroy.SelectedValue,
                                                                    ddlManu.SelectedValue,
                                                                    ddlBrand.SelectedValue,
                                                                    MyStateManager.DomainID,
                                                                    ddlSelected.SelectedValue);
        GridPager1.DataSource = ds.Tables[0];
        GridPager1.BindData();
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        GetProductsBind();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        WareHouseFromProductManager pm = new WareHouseFromProductManager();
        PRODUCT_SHIP_FROM psf = null;
        decimal _ProductID = 0;
        decimal _PRODUCT_LOCATION_ID = 0;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = GridView1.Rows[i].FindControl("chkInclude") as CheckBox;
            CheckBox cbxOnHold = GridView1.Rows[i].FindControl("IsOnHold") as CheckBox;
          
            if (cbox != null&&cbxOnHold!=null)
            {
                decimal.TryParse(GridView1.DataKeys[i].Values[0].ToString(), out _ProductID);
                decimal.TryParse(GridView1.DataKeys[i].Values[1].ToString(), out _PRODUCT_LOCATION_ID);
                if (_PRODUCT_LOCATION_ID > 0)
                {
                    psf = pm.Load(_PRODUCT_LOCATION_ID);
                   
                }
                else
                {
                    psf = new PRODUCT_SHIP_FROM();
                    psf.ProductID = _ProductID;
                    psf.SHIPFROMCODE = hdfWareHouseCode.Value;
                    psf.DOMAINID = MyStateManager.DomainID;
                    psf.INVENTORY = 100;
                }

                psf.LAST_UPDATER = MyStateManager.Name;
               psf.LAST_UPDATE_TIME = Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeDecimal(DateTime.Now);
               psf.ACTIVE = cbox.Checked;
               psf.ONHOLD = cbxOnHold.Checked ? "1" : "0";
               if (cbox.Checked||cbxOnHold.Checked)
                   pm.Save(psf);
               else
                   pm.Delete(psf);

              
            }
       }
        Response.Redirect("Warehouse_Locations_List.aspx");
    }
    //protected void ckbAll_CheckedChanged(object sender, EventArgs e)
    //{
    //    for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
    //    {
    //        CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("chkInclude");
    //        if (ckbAll.Checked == true)
    //        {
    //            cbox.Checked = true;
    //        }
    //        else
    //        {
    //            cbox.Checked = false;
    //        }
    //    }
    //}

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            //PRODUCTSHIPWITHPRODUCT rowView = (PRODUCTSHIPWITHPRODUCT)e.Row.DataItem;

            //bool selected = rowView.ACTIVE;

            //Control ctlCheckBox = e.Row.FindControl("chkInclude");

            //CheckBox cbxSelected = ctlCheckBox as CheckBox;

            //if (cbxSelected != null)
            //{
            //    if (selected)
            //    {
            //        cbxSelected.Checked = true; ;
            //    }
            //    else
            //    {
            //        cbxSelected.Checked = false;
            //    }
            //}
        }
    }
}
